package com.tmian.aiagent.controller;

import com.tmian.aiagent.SuperAgent.TManus;
import com.tmian.aiagent.app.DelicaciesApp;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
public class AiController {

    @Resource
    DelicaciesApp delicaciesApp;

    /**
     * 同步方式返回处理结果
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping("/delicaciesAPP/chat/sync")
    public String doChatWithAppSync(String message, String chatId){
        String result = delicaciesApp.doChat(message, chatId);

        return result;
    }

    /**
     * 流式输出结果
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "/delicaciesAPP/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> doChatWithAppStream(String message, String chatId){
        Flux<String> result = delicaciesApp.doChatByStream(message, chatId);

        return result;
    }

    @Resource
    private ToolCallback[] allTools;

    @Resource
    private ChatModel dashscopeChatModel;

    /**
     * 流式调用 Manus 超级智能体
     *
     * @param message
     * @return
     */
    @GetMapping("/manus/chat")
    public SseEmitter doChatWithManus(String message) {
        TManus tManus = new TManus(allTools, dashscopeChatModel);
        return tManus.runStream(message);
    }

}
